package com.thug;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Before;
import org.junit.Test;
import org.unitils.UnitilsJUnit4;
import org.unitils.dbunit.annotation.DataSet;
import org.unitils.dbunit.annotation.ExpectedDataSet;
import org.unitils.spring.annotation.SpringBean;

import com.thug.bo.Customer;
import com.thug.client.ProviderWs;
import com.thug.facade.CustomerFacade;

/**
 * Test class base, implementing tests for all cases.
 * 
 * @author THUGUERRE
 * @since 2011-03-16
 */
@DataSet({ "/database/dataset/main.xml" })
public abstract class AbstractTestCase extends UnitilsJUnit4 {

	private static final Log LOGGER = LogFactory.getLog(AbstractTestCase.class);

	// --- facade to use in tests
	@SpringBean("customerFacade")
	protected CustomerFacade facade = null;

	// --- data about the customer to get
	private static final int CUSTOMER_ID = 1;
	private static final String CUSTOMER_FIRST_NAME = "Chuck";
	private static final String CUSTOMER_LAST_NAME = "Norris";
	private static final String NEW_CUSTOMER_LAST_NAME = "Norris2";
	private static final int CUSTOMER_NB_PRODUCTS = 2;

	@Before
	public void setUp() {

		// --- allows controlling simulation of slow Web Services
		// --- should be to true for true demonstration
		// --- should be to false for development
		ProviderWs.setIsSlow(true);
	}

	@Test
	public void getCustomer() throws Exception {

		LOGGER.debug("start");

		LOGGER.info("First call to facade.");
		Customer customer = facade.getCustomer(CUSTOMER_ID);

		assertNotNull(customer);
		assertEquals(CUSTOMER_ID, customer.getId());
		assertEquals(CUSTOMER_FIRST_NAME, customer.getFirstName());
		assertEquals(CUSTOMER_LAST_NAME, customer.getLastName());
		assertEquals(CUSTOMER_NB_PRODUCTS, customer.getProducts().size());
		assertEquals(CUSTOMER_NB_PRODUCTS, customer.getAddresses().size());

		LOGGER.info("Second call to facade.");
		Customer customer2 = facade.getCustomer(CUSTOMER_ID);

		assertNotNull(customer2);
		assertEquals(CUSTOMER_ID, customer2.getId());
		assertEquals(CUSTOMER_FIRST_NAME, customer2.getFirstName());
		assertEquals(CUSTOMER_LAST_NAME, customer2.getLastName());
		assertEquals(CUSTOMER_NB_PRODUCTS, customer2.getProducts().size());
		assertEquals(CUSTOMER_NB_PRODUCTS, customer2.getAddresses().size());

		LOGGER.debug("end");
	}

	@Test
	@ExpectedDataSet({ "/database/dataset/expected-last-name-change.xml" })
	public void updateCustomerName() throws Exception {

		LOGGER.debug("start");

		facade.updateCustomerName(CUSTOMER_ID, NEW_CUSTOMER_LAST_NAME);

		LOGGER.debug("end");
	}
}
